package agendaBD;

/*
 * 
 */

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Conexao
{
    
    private static Conexao instance;
    
    private Connection connection;
    private Statement comando;
    private ResultSet resultado;
    private String usuario;
    private String senha;
   

    private Conexao () throws Exception
    {
        try
        {
            Class.forName ("oracle.jdbc.driver.OracleDriver");
        }
        catch(java.lang.ClassNotFoundException E)
        {
            System.out.println(E.toString());
            throw new Exception (E.toString());
        }
        try
        {
            String serverName = "172.16.12.9";//IP do FS-CEATEC
            String portNumber = "1521";//Porta para comunicação sql
            String sid = "bdsec12";//Nome da base de dados
            String url = "jdbc:oracle:thin:@172.16.12.9:1521:ORCL";
            String username = "bdsec19";
            String password = "liqw987";
            

            connection = DriverManager.getConnection(url, username, password);
            System.out.println("Conexão Estabelecida");
        }
        catch(SQLException E)
        {
            System.out.println(E.toString());
            throw new Exception (E.toString());
        }

        try
        {
            comando = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        }
        catch(SQLException E)
        {
            System.out.println("3");
            throw new Exception (Exception.class.getName());
        }

    }
    
    public static Conexao getInstance(){
        
        if(Conexao.instance == null){
            try {
                Conexao.instance = new Conexao();
            } catch (Exception ex) {
                //TODO LOG
                ex.printStackTrace();
            }
        }
        
        return Conexao.instance;
    }

    public ResultSet consultar (String comsql) throws Exception
    {
        try
        {
            resultado = comando.executeQuery(comsql);
        }

        catch (SQLException e)
        {
            System.out.println(e.toString());
            throw new Exception (e);
        }
        return resultado;
    }

    public boolean executar(String query) throws SQLException{
        
        return this.comando.execute(query);
 
    }
    
}